package com.yodawnla.roboAttack;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.particle.Particle;
import org.anddev.andengine.entity.particle.ParticleSystem;
import org.anddev.andengine.entity.particle.initializer.ColorInitializer;
import org.anddev.andengine.entity.particle.initializer.IParticleInitializer;
import org.anddev.andengine.entity.particle.modifier.AlphaModifier;
//import org.anddev.andengine.entity.particle.modifier.ColorInitializer;
import org.anddev.andengine.entity.particle.modifier.ColorModifier;
import org.anddev.andengine.entity.particle.modifier.ExpireModifier;
//import org.anddev.andengine.entity.particle.modifier.IParticleInitializer;
import org.anddev.andengine.entity.particle.modifier.ScaleModifier;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

public class Fireworks extends ParticleSystem
{

    private double mSpreading;
    
    public float mExplosionX;
    public float mExplosionY;
    public double mInitVel;
    public float mDecelPercent;
    public int mNumParticles;
    public int mNumParticlesPlusOne;
    public int mCurrentParticleIndex;
    public double vel;
    public double incr;
    public int mCameraWidth;
    public int mCameraHeight;
    Random mGenerator;
    
    
	public Fireworks(float fX, float fY, TextureRegion textureRegion, int iNumParticles, int iCameraWidth, int iCameraHeight)
	{
		super(fX, fY, 0, 0, (float)iNumParticles*8, (float)iNumParticles*8, iNumParticles, textureRegion);
		
        //initialize
        mDecelPercent = 75/100.0f;
        mInitVel = 180.0;
        mSpreading = 80.0;
        
        mGenerator = new Random();
        vel = mInitVel;
        incr = mSpreading/iNumParticles;
        mNumParticles = iNumParticles;
        mNumParticlesPlusOne = mNumParticles+1;
        mCurrentParticleIndex = 0;
        mExplosionX = fX;
        mExplosionY = fY;
        mCameraWidth = iCameraWidth;
        mCameraHeight = iCameraHeight;

        this.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
        this.addParticleInitializer(new ColorInitializer(0.1f, 1,  0.1f, 1, 0.1f, 1));
        this.addParticleModifier(new ScaleModifier(0.3f, 0.3f, 0.0f, 1.0f));
        this.addParticleModifier(new ExpireModifier(1.8f));
 //       this.addParticleModifier(new ColorModifier(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f));
        this.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 0.0f, 1.5f)); 
//        this.addParticleModifier(new IParticleModifier()
//        {
//            @Override
//            public void onInitializeParticle(Particle pParticle)
//            {
//                //do nothing
//            }
//
//            @Override
//            public void onUpdateParticle(Particle pParticle)
//            {
//                if (pParticle.getX() >= mCameraWidth)
//                {
//                    //prevent the particle from going off-screen
//                    pParticle.setVelocity(0.0f, 0.0f);
//                    pParticle.setAcceleration(0.0f, 0.0f);
//                }
//
//                //bounce the particles inside the screen area
//                if (pParticle.getX() < 1)
//                {
//                    pParticle.setVelocity( Math.abs(pParticle.getVelocityX()), pParticle.getVelocityY() );
//                    pParticle.setAcceleration( -(pParticle.getAccelerationX()), pParticle.getAccelerationY() );
//                }
//                else
//                    if( (pParticle.getX() + pParticle.getWidthScaled() ) > (mCameraWidth-2))
//                    {
//                        pParticle.setVelocity( -(pParticle.getVelocityX()), pParticle.getVelocityY() );
//                        pParticle.setAcceleration( Math.abs(pParticle.getAccelerationX()), pParticle.getAccelerationY() );
//
//                    }
//
//                if (pParticle.getY() < 1)
//                {
//                    pParticle.setVelocity( pParticle.getVelocityX(), Math.abs(pParticle.getVelocityY()) );
//                    pParticle.setAcceleration( pParticle.getAccelerationX(), -(pParticle.getAccelerationY()) );
//                }
//                else
//                    if( (pParticle.getY() + pParticle.getHeightScaled() ) > (mCameraHeight-2) )
//                    {
//                        pParticle.setVelocity( pParticle.getVelocityX(), -(pParticle.getVelocityY()) );
//                        pParticle.setAcceleration( pParticle.getAccelerationX(), Math.abs(pParticle.getAccelerationY()) );
//                    }
//            }
//        });
        
        this.addParticleInitializer(new IParticleInitializer()
        {
        	@Override
        	public void onInitializeParticle(Particle pParticle)
        	{
        		// Calculate particle's departure angle in terms of degrees.
        		int ang = mGenerator.nextInt(359);
        		
        		//make some particles faster than others, but always reset vel after all particles done
         		mCurrentParticleIndex++;
        		if (mCurrentParticleIndex == mNumParticlesPlusOne)
        		{
        			vel = mInitVel;
        			mExplosionX = mGenerator.nextInt(mCameraWidth-150)+150;
        			mExplosionY = mGenerator.nextInt(mCameraHeight-150)+150;
        			mCurrentParticleIndex=1;
        		}
           		vel -= incr;
           		pParticle.setPosition(mExplosionX, mExplosionY);
        		
        		float fVelocityX = (float)(Math.cos(Math.toRadians(ang)) * vel);
        		float fVelocityY = (float)(Math.sin(Math.toRadians(ang)) * vel);
//        		pParticle.setVelocity(fVelocityX, fVelocityY);
        		pParticle.getPhysicsHandler().setVelocity(fVelocityX, fVelocityY);
        		
        		// calculate air resistance that acts opposite to particle velocity
        		float fVelXopposite = TogglePosNeg(fVelocityX);
        		float fVelYopposite = TogglePosNeg(fVelocityY);
        		
        		// x% of deceleration is applied (that is opposite to velocity)
//        		pParticle.setAcceleration(fVelXopposite * mDecelPercent, fVelYopposite * mDecelPercent);
        		pParticle.getPhysicsHandler().accelerate(fVelXopposite * mDecelPercent, fVelYopposite* mDecelPercent);
        	}

        	//private float TogglePosNeg(float fInputNumber)
        	// what it does: makes a +ve number -ve, and -ve number +ve
        	private float TogglePosNeg(float fInputNumber)
        	{
        		return(fInputNumber - (2*fInputNumber));
        	}
        	
        });
	}
	
 
}